<?php

namespace App\Store\Services\Message;

class Ihuyi
{
    public $config = array(
        'apiid' => 'sms-yx7096737',
        'apikey' => '4my6aQzzdZRCVkP',
        'send_url' => "http://api.ihuyi.com/sms-yx/v1/batchSend",
    );

    public function __construct($config = array())
    {
        if ($config) {
            $this->config = $config;
        }
    }

    public  function post($curlPost, $url)
    {
        if(is_array($curlPost)){
            $curlPost = json_encode($curlPost);
        }
        $curl = curl_init();
        $header = array();
        $header[] = "Content-type: application/json";//定义content-type为json
        $header[] = "Content-Length: " . strlen($curlPost);//定义content-Length
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);//超时时间,最好不要太小，否则服务响应慢会丢包
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($curl, CURLOPT_NOBODY, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        //curl_setopt($curl, CURLOPT_HEADER, true);

//        if (strpos($url, "https") === 0){//访问SSL协议的URL https://
//            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 是否验证ssl证书
//            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 0表示不检查证书；1表示检查证书中是否有CN(common name)字段；2表示在1的基础上校验当前的域名是否与CN匹配
//        }

        $res = curl_exec($curl);
        var_dump($res);
        curl_close($curl);
        return $res;
    }

    //发送短信
    public function send($mobile, $content, $product_id, $sign_name, $send_time = '', $request_id = '',$template_id = 0,$template_var = array())
    {
        $target = $this->config['send_url'];
        $timestamp = time();
        if (empty($request_id)) {
            $request_id = time() . '_' . random_int(11111, 99999);
        }
        $signature = 'api_id=' . $this->config['apiid'] . '&' . 'api_key=' . $this->config['apikey'] . '&' . 'request_id=' . $request_id . '&' . 'timestamp=' . $timestamp;
        $post_data = array(
            'api_id' => $this->config['apiid'],
            'signature' => md5($signature),
            'phone' => $mobile,
            'sign_name' => $sign_name,
            'request_id' => $request_id,
            'timestamp' => $timestamp,
            'product_id' => $product_id,
            'send_time' => $send_time
        );
        var_dump($post_data);
        if(!$content && (!$template_id || empty($template_var))) {
            return ['缺少参数'];
        }
        if($content) {
            $post_data['content'] = $content;
        }
        if($template_id >0 || !empty($template_var)) {
            $post_data['template_id'] = $template_id;
            $post_data['template_var'] = $template_var;
        }
        try {
//            $this->log('send', $post_data);
            $res = $this->post($post_data, $target);
            var_dump($res);
            if($res){
                return json_decode($res, true);
            }

            return ['请求失败'];
        } catch (\Exception $e) {
            return [$e->getMessage()];
        }
    }
}
